package com.minova.demo.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.minova.demo.entity.TzBasket;
import com.minova.demo.entity.TzOrder;
import com.minova.demo.entity.TzUserAddr;
import com.minova.demo.service.ITzBasketService;
import com.minova.demo.service.ITzOrderService;
import com.minova.demo.service.ITzUserAddrService;
import com.minova.demo.service.MergeService;

import net.sf.jsqlparser.statement.select.KSQLWindow.TimeUnit;

@RestController
@RequestMapping("/com/minova/demo/qingqiang")
public class pengQiangController {
	
	@Autowired
	private ITzBasketService basketService;
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	@Autowired
	private ITzUserAddrService userAddrService;
	
	@Autowired
	private ITzOrderService orderService;
	
	@Autowired
	private MergeService merge;
	
	
	//查找购物车
	//@Cacheable(value="basketInfo")
	@RequestMapping(value="basketInfo")
	public List<TzBasket> basketInfo() throws InterruptedException{
		
		String userId = "51540df5255e4d22903b0f83921095ff";

//		if(redisTemplate.opsForList().range(userId,0,-1)!=null) {
//			return (List<TzBasket>)redisTemplate.opsForList().range(userId,0,-1).get(0);
//		}

		redisTemplate.opsForList().leftPush(userId, basketService.selectBasketByUserId(userId) );
//		redisTemplate.expire("lll", 60*60*24, java.util.concurrent.TimeUnit.MILLISECONDS);
//		Thread.sleep(2000);
//		System.out.println(redisTemplate.getExpire("lll"));
//		System.out.println(redisTemplate.opsForList().range("lll",0,-1));
//		Thread.sleep(2000);
//		System.out.println(redisTemplate.getExpire("lll"));
//		Thread.sleep(2000);
//		System.out.println(redisTemplate.getExpire("lll"));
//		Thread.sleep(2000);
//		System.out.println(redisTemplate.getExpire("lll"));
//		Thread.sleep(2000);
//		System.out.println(redisTemplate.getExpire("lll"));
//		
//		Thread.sleep(2000);
		//System.out.println(basketService.selectBasketByUserId(userId).get(0).getTzProd().getPic());
		return basketService.selectBasketByUserId(userId);
	}
	
	//修改购物车里商品个数
	//@CachePut(value = "basketModfiy", key = "#user.id") 
	@RequestMapping(value="savebasket")
	public boolean savebasket(String tzBasket) {
		//String userId = "51540df5255e4d22903b0f83921095ff";
		boolean flag = true;
		TzBasket tzBaskets = JSON.parseObject(tzBasket, new TypeReference<TzBasket>(){});
//		List<TzBasket> blist = basketService.selectBasketByUserId(userId);	//(List<TzBasket>)redisTemplate.opsForList().range(userId,0,-1).get(0);
//		int index = -1;
//		for (int i = 0; i < blist.size(); i++) {
//			TzBasket basket = blist.get(i);
//			if(tzBaskets.getBasketId().equals(basket.getBasketId())) {
//				index = i;
//				break;
//			}
//		}
//		blist.get(index).setBasketCount(tzBaskets.getBasketCount());
//		redisTemplate.opsForList().leftPush(userId,blist);
		try {

			merge.updateBasketBasketCount(tzBaskets);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = false;
		}
		
		return flag;
	}  
	
	/**
	 * 
	 * @param basket
	 */
	@RequestMapping("deleteBasket")
	public boolean deleteBasket(String basket) {
		//String userId = "51540df5255e4d22903b0f83921095ff";
		boolean flag = true;
		List<TzBasket> baskets = JSON.parseArray(basket, TzBasket.class);
		System.out.println(baskets);
//		List<TzBasket> blist = (List<TzBasket>)redisTemplate.opsForList().range(userId,0,-1).get(0);
//		List<Long> ids = new ArrayList<>();
//		for (TzBasket tzBasket : baskets) {
//			ids.add(tzBasket.getBasketId());
//			blist.remove(tzBasket);
//		}
		//redisTemplate.opsForList().leftPush(userId,blist);
		//basketService.removeByIds(ids);
		try {
			merge.removeBasket(baskets);
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	@RequestMapping("test")
	public String test(String test) {
		return "测试";
	}
	
	@RequestMapping(value="Settlement")
	public Map<String,Object> settlement(String ids){
		String userId = "51540df5255e4d22903b0f83921095ff";
		Map<String,Object> map = new HashMap<>();
		map.put("prod", basketService.selectById(ids));
		map.put("addr", userAddrService.selectByUserId(userId));
		return map;
	}
	
	@RequestMapping(value="saveOrder")
	public boolean saveOrder(String order,String ids,String prod) {
		boolean flag = true;
		List<TzOrder> orderlist = JSON.parseArray(order, TzOrder.class);
		List<TzBasket> baskets = JSON.parseArray(prod, TzBasket.class);
		String userId = "51540df5255e4d22903b0f83921095ff";
		for (TzOrder tzOrder : orderlist) {
			tzOrder.setUserId(userId);
		}
		List<Long> basketIds = JSON.parseArray(ids, Long.class);
		try {
			merge.saveOrder(orderlist,basketIds,baskets);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
}
